package com.shelpe.services.scheduler.item.order;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.shelpe.services.scheduler.repository.UsersRepository;

@Component("orderCustomerReaderTest")
@Scope("step")	
public class OrderCustomerReader implements ItemReader<Long>{
	
	private static final Logger logger = LoggerFactory.getLogger(OrderCustomerReader.class);
	
	private Long userNum;
	
    private final Long pageNo;
    
    private final Long pageSize;
    
    private final Long subIndex;
    
    private final Long subPages;
    
    private final Long userId;
    
	@Autowired
	private UsersRepository userRep;
	
	@Autowired
	public OrderCustomerReader(@Value("#{jobParameters['pageNo']}")final Long pageNo, @Value("#{jobParameters['pageSize']}")final Long pageSize,
    		@Value("#{jobParameters['subIndex']}")final Long subIndex, @Value("#{jobParameters['subPages']}")final Long subPages,
    		@Value("#{jobParameters['userId']}")final Long userId){
	
			this.pageNo = pageNo;
	        this.pageSize = pageSize;
	        this.subIndex = subIndex;
	        this.subPages = subPages;
	        this.userId = userId;		
	        this.userNum = null;
	}

	@Override
	public Long read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
		// TODO Auto-generated method stub
		Long index = 0L; 
		
		if(userNum == null && userId == null && pageNo != null && pageNo.equals(index) 
				&& subIndex != null && subIndex.equals(index)){//第一个线程处理
			
			userNum = userRep.count();
			logger.info("OrderCustomerReader userNum {}", userNum);
			
			return userNum;
		}
		
		return null;
	}
	
	
	

}
